package com.xd.core.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xd.common.constant.SysConstant;
import com.xd.common.exception.XkdException;
import com.xd.core.company.entity.Company;
import com.xd.core.company.entity.CompanyAccount;
import com.xd.core.note.ICacheService;
import com.xd.core.system.entity.*;
import com.xd.core.system.service.H5MyUserDetailsService;
import com.xd.core.system.service.IH5SysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class H5MyUserDetailsServiceImpl implements H5MyUserDetailsService {
    @Autowired
    private IH5SysUserService ih5SysUserService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ICacheService iCacheService;

    @Override
    public UserDetails loadUserByUsername(String phone) {
        //（认证，即校验该用户是否存在）查询用户信息
        LambdaQueryWrapper<H5SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(H5SysUser::getPhone, phone);
        H5SysUser user = ih5SysUserService.getOne(queryWrapper);
        //如果没有查询到用户
        if (Objects.isNull(user)) {
            throw new XkdException("账号不存在");
        }
        //判断账号是否已删除
        String delFlag = user.getDelFlag();
        if (delFlag.equals(SysConstant.XKD_DEL_FLAG_DELETED)) {
            throw new XkdException("账号已删除");
        }
        //获取数据权限
        //把数据封装成UserDetails返回
        return new H5MyLoginUser(user);
    }
}
